﻿// -----------------------------------------------------------------------
// <copyright file="UserViewModel.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PlaySongs
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Windows.Input;
    
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class UserViewModel : INotifyPropertyChanged
    {
        #region Construction
        /// <summary>
        /// Constructs the default instance of a SongViewModel
        /// </summary>
        public UserViewModel()
        {
            _user = new User { UserName = "Unknown", Password = "Unknown" };
        }
        #endregion

        #region Members
        User _user; 
        #endregion

        #region Properties
        public User User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region Commands
        void Login()
        {
            if ( _user.UserName == "a" && _user.Password == "b" )
            {
                //close Login dialog and open PlaySong dialog
                CloseDialog();

            }
            else
            {
                //display message on view
                ShowMessage( "fail" );
            }
        }

        bool CanLogin()
        {
            return true;
        }

        public ICommand LoginUser { get { return new RelayCommand( Login, CanLogin ); } }

        bool CanClose()
        {
            return true;
        }

        public ICommand CloseView { get { return new RelayCommand( CloseDialog, CanClose ); } }

        #endregion

        public event Action RequestClose;
        public event SendMessage DisplayMessage;

        public delegate void SendMessage( object obj, string s );

        public virtual void CloseDialog()
        {
            if ( RequestClose != null )
            {
                RequestClose();
            }
        }

        public virtual void ShowMessage(string a)
        {
            if ( DisplayMessage != null )
            {
                DisplayMessage(this, a);
            }
        }
    }
}
